package generator.common;


import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import generator.entity.dataModel;
import lombok.Setter;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;


public class FreeMarker {
    private final Configuration configuration = new Configuration(Configuration.getVersion());
//    @Setter
//    private String outPath;
    public FreeMarker() {
        try {
            configuration.setDirectoryForTemplateLoading(new File("src/main/resources/freemark"));
            configuration.setDefaultEncoding("utf-8");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void makeTemplate(String templateName, Object model, String outPath) {
        try {
            Template template = configuration.getTemplate(templateName);
            FileWriter out = new FileWriter(new File(outPath));
            Map Model = new HashMap<>();
            Model.put("dataModel",model);
            template.process(Model,out);
            out.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TemplateException e) {
            throw new RuntimeException(e);
        }
    }
}
